package com.jd.ty.dao.util;

import java.util.Date;

import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.util.CommonUtils;

public class HqlCriterions {

	private String hql;
	private String sort;
	public  HqlCriterions(String hql) {
		this.hql=hql;

	}
	public  HqlCriterions() {

	}
    public HqlCriterions and(String expression) {
    	if (expression == null || expression.trim().length() == 0) {
    		return this;
    	}
        if (hql == null) {
        	hql = "";
        } else {
        	hql += " And ";
        }
//        expression = expression.replaceAll("'", "''");
        hql += "(" + expression + ")";
        return this;
    }
    public HqlCriterions or(String expression) {
    	if (expression == null || expression.trim().length() == 0) {
    		return this;
    	}
        if (hql == null) {
        	hql = "";
        } else {
        	hql += " OR ";
        }
//        expression = expression.replaceAll("'", "''");
        hql += "(" + expression + ")";
        return this;
    }
    public HqlCriterions and(String fieldName, Object value, String condition) {
        if (hql == null) {
        	hql = "(";
        } else {
        	hql += " And (";
        }
        if (value instanceof String) {
        	value = ((String) value).replaceAll("'", "''");
        	if (condition.equalsIgnoreCase(QueryConstants.LIKE)) {
                hql += "upper(this." + fieldName + ") " + condition;
            	hql += " '%" + value.toString().toUpperCase() + "%'"; 
        	} else {
                hql += "upper(this." + fieldName + ")" + condition;
            	hql += "'" + value.toString().toUpperCase() + "'"; 
        	}
        } else if (value instanceof Date) {
        	if (condition.equals(QueryConstants.GE) || condition.equals(QueryConstants.GT)) {
                hql += "this." + fieldName  + condition + "'" + CommonUtils.date2String((Date) value)+"'" ;
        	} else {
        		hql += "this." + fieldName  + condition + "'" + CommonUtils.date2String((Date) value)+"'" ;
        	}
        } else {
            hql += "this." + fieldName + condition;
        	hql += value;
        }
        hql += ")";
        return this;
    }
    public HqlCriterions or(String fieldName, Object value, String condition) {
        if (hql == null) {
        	hql = "(";
        } else {
        	hql += " OR (";
        }
        if (value instanceof String) {
        	value = ((String) value).replaceAll("'", "''");
        	if (condition.equalsIgnoreCase(QueryConstants.LIKE)) {
                hql += "upper(this." + fieldName + ") " + condition;
            	hql += " '%" + value.toString().toUpperCase() + "%'"; 
        	} else {
                hql += "upper(this." + fieldName + ")" + condition;
            	hql += "'" + value.toString().toUpperCase() + "'"; 
        	}
        } else if (value instanceof Date) {
        	if (condition.equals(QueryConstants.GE) || condition.equals(QueryConstants.GT)) {
                hql += "this." + fieldName  + condition + CommonUtils.date2String((Date) value);
        	} else {
        		hql += "this." + fieldName + condition  + CommonUtils.date2String((Date) value);
        	}
        } else {
            hql += "this." + fieldName + condition;
        	hql += value;
        }
        hql += ")";
        return this;
    }
    public HqlCriterions sort(String fieldName, String sort) {
        this.sort = " " + fieldName + " " + sort;
        return this;
    }
    
//	public String getHql() {
//		return hql;
//	}
//
//	public void setHql(String hql) {
//		this.hql = hql;
//	}
	
    public String getHqlString() {
//    	return hql.toString();
    	if (hql == null || hql.length() == 0) {
    		return null;
    	} else {
    		return hql.toString();
    	}
    }
    
    public String getSortString() {
//    	return sort.toString();
    	if (sort == null || sort.length() == 0) {
    		return null;
    	} else {
    		return sort.toString();
    	}
    }
    
    public void clear() {
    	this.hql = null;
    	this.sort = null;
    }

}
